package com.ceair.flight.refund.service.audit.impl;

import com.ceair.flight.refund.dto.ResponseConst;
import com.ceair.flight.refund.dto.appmoblie.RefundApplyDto;
import com.ceair.flight.refund.dto.audit.AuditResult;
import com.ceair.flight.refund.dto.audit.AuditResultRes;
import com.ceair.flight.refund.dto.audit.BaseAuditReq;
import com.ceair.flight.refund.dto.external.ServiceException;
import com.ceair.flight.refund.location.dt.enums.RefundResConst;
import com.ceair.flight.refund.service.audit.ISecondAudit;
import com.ceair.flight.refund.service.audit.ISecondAuditService;
import com.ceair.flight.refund.service.uatp.IGenRefNoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Service("secondAudit")
@Path("/secondAudit")
public class SecondAuditImpl implements ISecondAudit {

    private static final Logger LOG = LoggerFactory.getLogger(SecondAuditImpl.class);
    @Autowired
    private ISecondAuditService secondAuditService;
    @Autowired
    private IGenRefNoService genRefNoService;
    

    @Override
    @POST
    @Path("/doPassAudit")
    @Consumes({"application/json"})
    @Produces(MediaType.APPLICATION_JSON)
    public AuditResultRes doPassAudit(BaseAuditReq auditReq) {
        LOG.info("二审确认通过业务开始...");
        AuditResultRes res = new AuditResultRes();
        AuditResult result;
        try {
            //手动二审
            RefundApplyDto refundApply = secondAuditService.doAuditPass(auditReq, false);
            LOG.info("二审确认通过业务完成");
            result = new AuditResult(false, null);
            res.setAuditResult(result);
            try {
                //生成退票单号和更改退票申请单状态
                genRefNoService.doGenRefNo(refundApply);
            } catch (IllegalStateException e) {
            	res.setResultCode(RefundResConst.ILL_STATE_CODE);
            	res.setResultMsg(RefundResConst.getExceptionMsg(e.getMessage(), RefundResConst.ILL_STATE_MSG));
            	return res;
            } catch (ServiceException e) {
            	res.setResultCode(e.getMessageCode());
            	res.setResultMsg(RefundResConst.getExceptionMsg(e.getMessage(),e.getMessage()));
            	return res;
            } catch (Exception e) {
            	res.setResultCode(RefundResConst.OTHER_REASON_CODE);
            	res.setResultMsg(RefundResConst.getExceptionMsg(e.getMessage(), RefundResConst.OTHER_REASON_CODE));
            	return res;
            }
        } catch (Exception e) {
            result = new AuditResult(true, e.getMessage());
            LOG.error("Second audit pass failed for refund apply {}", auditReq.getApplyId(), e);
        	res.setResultCode(RefundResConst.SECOND_PASS_ABNORMAL_CODE);
        	res.setResultMsg(RefundResConst.SECOND_PASS_ABNORMAL_MSG + auditReq.getApplyId());
        	return res;
//            throw new ServiceException("004060001001", "退票记录" + auditReq.getApplyId() + "二审通过异常");
        }
    	res.setResultCode(ResponseConst.SUCCESS_CODE);
    	res.setResultMsg(ResponseConst.SUCCESS_MSG);
        return res;
    }

    @Override
    @POST
    @Path("/doRefuseAudit")
    @Consumes({"application/json"})
    @Produces(MediaType.APPLICATION_JSON)
    public AuditResultRes doRefuseAudit(BaseAuditReq auditReq) {
        LOG.info("二审确认拒绝业务开始...");
        AuditResultRes res = new AuditResultRes();
        AuditResult result = new AuditResult();
        res.setAuditResult(result);
        try {
            secondAuditService.doAuditRefuse(auditReq);
            LOG.info("二审确认拒绝业务完成");
        } catch (Exception e) {
            result = new AuditResult(true, e.getMessage());
            LOG.error("Second audit refuse failed for refund apply {}", auditReq.getApplyId(), e);
        	res.setResultCode(RefundResConst.SECOND_REFUSE_ABNORMAL_CODE);
        	res.setResultMsg(RefundResConst.SECOND_REFUSE_ABNORMAL_CODE + auditReq.getApplyId());
//            throw new ServiceException("004060002001", "退票记录" + auditReq.getApplyId() + "二审拒绝异常");
        }
    	res.setResultCode(ResponseConst.SUCCESS_CODE);
    	res.setResultMsg(ResponseConst.SUCCESS_MSG);
        return res;
    }
}
